<template>
  <div style="width: 100%;height: 100%;">
    <div class="filter-container">
      <span style="float:left;width:20%;">
        <el-input @keyup.enter.native="handleFilter" v-model="listQuery.conKey" placeholder="请输入关键字"
          class="filter-item" />
      </span>
      <span style="float:left;margin-left: 10px;">
        <el-button @click="handleFilter" class="filter-item" type="primary" v-waves icon="el-icon-search">查询</el-button>
        <span v-if="menuId != ''">
          <el-button @click="handleCreate" :disabled="btnDisabled" class="filter-item" type="primary" v-waves
            icon="el-icon-plus">添加
          </el-button>
        </span>
      </span>
    </div>
    <!-- 表格 -->
    <el-table :data="list" v-loading="listLoading" :height="tableHeight" border fit highlight-current-row
      style="width: 100%;">
      <el-table-column width="60px" align="center" label="序号">
        <template slot-scope="scope">
          <span>{{ scope.$index +1 + (listQuery.pageSize*(listQuery.page-1))}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="apiCode" label="接口资源编码"></el-table-column>
      <el-table-column align="center" prop="apiName" label="接口资源名称"></el-table-column>
      <el-table-column align="center" prop="apiUri" label="接口资源URI"></el-table-column>
      <el-table-column label="请求方法" align="center">
        <template slot-scope="scope">
          <span>{{getNameByCode(scope.row.apiMethod, allDictData.methodOps)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录日志" width="90px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.log == 0 ? "否" : "是"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="70px" align="center">
        <template slot-scope="scope">
          <span>{{getNameByCode(scope.row.status, allDictData.statusOps)}}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row.id)" size="small" type="success" v-waves icon="el-icon-edit">修改
          </el-button>
          <el-button @click="handleDelete(scope.row.id)" size="small" type="danger" v-waves icon="el-icon-delete">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加修改弹出框页面组件 -->
    <add-update v-if="showAdd" @flushList="flushList" @closeDialog="closeDialog" :restPath="restPath" :showAdd="showAdd"
      :btnType="btnType" :rowId="rowId" :allDictData="allDictData" :menuId="menuId" />

  </div>
</template>

<script>
  /* mixins */
  import pageListMixin from '@/mixins/pageList/pageListMixin.js'

  import {
    pageList,
    getDictData
  } from '@/api/common'

  /* 组件 */
  import addUpdate from './addUpdate.vue'

  /* 工具 */
  import * as common from '@/utils/base/common'
  import {
    getNameByCode
  } from '@/utils/dictData'

  export default {
    /* 按钮接口资源 */
    name: 'resourceApi',
    data() {
      return {
        tableHeight: (parseInt(parseInt(document.documentElement.clientHeight)) / 7) + 'px',

        restPath: 'sys/resourceApi',
        listQuery: {
          page: 1,
          pageSize: 10,
          conKey: '',
        },
        listLoading: false,

        allDictData: {
          statusOps: []
        },

        menuId: '',
      }
    },
    mixins: [pageListMixin],
    components: {
      'add-update': addUpdate,
    },
    methods: {

      initData() {
        this.getAllDictData()
      },

      refreshData(menuId) {
        this.menuId = menuId
        this.getApiList(menuId)
      },

      getAllDictData() {
        const params = {
          dictIds: "REQUEST_METHOD,STATUS"
        }
        getDictData(params).then(response => {
          let data = response.data
          this.allDictData.methodOps = data.REQUEST_METHOD
          this.allDictData.statusOps = data.STATUS
        }).catch(err => {
          common.message(this, 'error', 2, '获取数据字典失败!')
          console.error(err)
        })
      },

      getNameByCode(val, dictMap) {
        return getNameByCode(val, dictMap)
      },

      /**
       * 后台获取列表数据
       */
      getApiList(menuId) {
        this.listLoading = true
        this.listQuery.menu_id = menuId // 关联的菜单主键ID
        pageList(this.restPath, this.listQuery).then(response => {
          this.list = response.data.records
          this.total = response.data.total
          this.listLoading = false
        }).catch(err => {
          this.listLoading = false;
          this.btnDisabled = true // 按钮禁用
          console.error(err)
        })
      },

      resetData() {
        this.menuId = ''
        this.list = null
      },

    }
  }
</script>

<style scoped>
  @import "~@/styles/list.css";
</style>
